package crafex.resource.items.gunparts;

import crafex.engine.tools.IntVertex2D;
import crafex.engine.tools.Range;
import crafex.engine.tools.Tools;

public class Stock extends GunPart{
	
	public IntVertex2D connectReceiver;
	public String s_connectReceiver = "-connectReceiver=";
	
	public float accuracy;
	public Range g_accuracy;
	public String s_startAcccuracy = "-startaccuracy=";
	public String s_endAcccuracy = "-endaccuracy=";

	public Stock(int id) {
		super(id, new IntVertex2D(2, 2));
	}
	
	@Override
	public String[] save(String name){
		String[] data = new String[9];
		data[0] = name;
		data[1] = s_name + this.name;
		data[2] = s_brandID + brandID;
		data[3] = s_image + imagefilename;
		data[4] = s_rareChance + rareChance;
		data[5] = s_connectReceiver + connectReceiver.getX() + "," + connectReceiver.getY();
		data[6] = s_startAcccuracy + g_accuracy.getLowest();
		data[7] = s_endAcccuracy + g_accuracy.getHighest();
		data[8] = s_waterproof + g_waterproof;
		
		
		
		
		return data;
	}
	
	@Override
	public void load(String[] save) {
		for(int i = 0; i < save.length; i++){
			if(save[i].contains(s_name)){
				name = save[i].substring(s_name.length());
			}
			if(save[i].contains(s_startAcccuracy)){
				g_accuracy.setLowest(Integer.getInteger(save[i].substring(s_startAcccuracy.length())));
			}
			if(save[i].contains(s_endAcccuracy)){
				g_accuracy.setHighest(Integer.getInteger(save[i].substring(s_endAcccuracy.length())));
			}
			if(save[i].contains(s_waterproof)){
				g_waterproof = Boolean.getBoolean(save[i].substring(s_waterproof.length()));
			}
			if(save[i].contains(s_rareChance)){
				rareChance = Integer.getInteger(save[i].substring(s_rareChance.length()));
			}
			if(save[i].contains(s_brandID)){
				brandID = save[i].substring(s_brandID.length());
			}
			if(save[i].contains(s_image)){
				imagefilename = save[i].substring(s_image.length());
			}
			if(save[i].contains(s_connectReceiver)){
				connectReceiver.setX(Integer.getInteger(save[i].substring(s_connectReceiver.length(), save[i].indexOf(","))));
				connectReceiver.setY(Integer.getInteger(save[i].substring(save[i].indexOf(",")+1)));
			}
		}
	}

	@Override
	public void generate(int level) {
		float temp = (float) (g_accuracy.getHighest() - g_accuracy.getLowest() * 0.25);
		accuracy = (float) (Tools.getRandomDouble(g_accuracy.getLowest(), (g_accuracy.getHighest() - temp)) * (level * 0.2));
		if(Tools.getRandomInt(0, rareChance) == (int) rareChance/2){
			accuracy += Tools.getRandomDouble(0, temp);
		}
	}

}
